package com.ymatou.shoppingcart.api.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.shoppingcart.api.parameter.GetShoppingCartItemsForOrderBean;
import com.ymatou.shoppingcart.api.service.GetShoppingCartItemsForOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.data.Price;
import com.ymttest.business.database.ActivityInfo;
import com.ymttest.business.database.ProductInfo;
import com.ymttest.business.service.ShoppingCartCallServices;
import com.ymttest.common.entitie.Activity;
import com.ymttest.common.entitie.Catalog;
import com.ymttest.common.entitie.Delivery;
import com.ymttest.common.entitie.Seller;
import com.ymttest.common.entitie.ShoppingCartItems1;
import com.ymttest.common.entitie.ShoppingCartItems2;
import com.ymttest.common.entitie.Total;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 查询获取购物车拆单后信息 FreeMaker Template自动生成代码
 */
// 根据开发反馈次接口目前未被前端使用。对应场景由其他功能模块完成。
// 这个接口没人用目前废弃状态
@TestSuite
public class Ts_GetShoppingCartItemsForOrder {
	private static GetShoppingCartItemsForOrderBean getitemsorderbean;
	private static GetShoppingCartItemsForOrderCall getitemsordercall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询获取购物车拆单后信息");
	}

	@Before
	public void caseUp() {
		getitemsorderbean = new GetShoppingCartItemsForOrderBean();
		getitemsordercall = new GetShoppingCartItemsForOrderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getitemsordercall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_001() {
		Logger.start(true,"查询获取购物车拆单后信息成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			// Logger.debug(activityInfo);
			Logger.comment("activityInfo：" + activityInfo);
			// Logger.debug("productinfomap:"+productinfomap);
			// Logger.debug("activityInfo"+activityInfo);
			Logger.comment("setUserID：" + uid);
			getitemsorderbean.setUserID(uid);
			// 310101 上海 黄浦区
			Logger.comment("setAreaCode：310101");
			getitemsorderbean.setAreaCode("310101");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemsordercall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemsordercall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			// 因为只购买了一个商品，所以商品信息都在ShoppingCartItems[0] 中
			ShoppingCartItems1 scinfo = getitemsordercall.getShoppingCartItems().get(0);
			// seller信息验证
			Seller seller = scinfo.getSeller();
			Logger.verifyEquals(productinfomap.get("isellerid"), seller.getID(), "验证sellerid");
			Logger.verifyEquals(productinfomap.get("loginid"), seller.getName(), "验证sellername");
			// sid 1362789 的 Country是2 暂时固定
			Logger.verifyEquals(2, seller.getCountry(), "验证Country");
			// 第二层ShoppingCartItems
			ShoppingCartItems2 scinfo2 = scinfo.getShoppingCartItems().get(0);
			// 新建商品只加了一个
			Logger.verifyEquals(1, scinfo2.getCount(), "验证Count");
			Logger.verifyEquals(true, scinfo2.isAvailable(), "验证isAvailable");
			Logger.verifyEquals(true, scinfo2.isSelected(), "验证isSelected");
			Logger.verifyNotNull(scinfo2.getCreatedTime(), "验证CreatedTime非空");
			Logger.verifyNotNull(scinfo2.getLastModifiedTime(), "验证LastModifiedTime非空");
			Catalog ca = scinfo2.getCatalog();
			// 验证 Catalog
			Logger.verifyEquals(productinfomap.get("scatalogid"), ca.getID(), "验证catalogid");
			Logger.verifyEquals(productinfomap.get("sproductid"), ca.getProductId(), "验证ProductId");
			Logger.verifyEquals(productinfomap.get("sProduct"), ca.getName(), "验证Name");
			Logger.verifyEquals(Double.valueOf(productinfomap.get("fQuotePrice").toString()), ca.getPrice(), "验证Price");
			Logger.verifyEquals(productinfomap.get("inum"), ca.getNum(), "验证inum");
			// limit规则？
			Logger.verifyEquals(productinfomap.get("ilimitnum"), ca.getLimitNum(), "验证limitnum");

			Logger.verifyEquals(productinfomap.get("sPicUrl"), ca.getPicture(), "验证PicUrl");
			// Status 对应什么数据？
			// 验证activity
			Activity act = ca.getActivity();
			Logger.verifyEquals("自动化测试新人活动特惠", act.getName(), "验证activeName");
			// compareTo为0表示2者数字一致，可以避免精度问题导致的对比失败
			Logger.verifyEquals(0, Price.getOneActprice(activityInfo, 0, false).compareTo(act.getPrice()),
					"验证Price是否正确:" + act.getPrice());
			// Delivery
			Delivery deli = ca.getDelivery();
			// 默认3 现在写死
			Logger.verifyEquals(3, deli.getDeliveryMethod(), "验证DeliveryMethod");
			Logger.verifyEquals(0,
					new BigDecimal(productinfomap.get("Fflight").toString()).compareTo(deli.getFreight()),
					"验证Freight是否正确:" + deli.getFreight());
			Logger.verifyNotNull(deli.getFreightDescription(), "验证FreightDescription非空");
			// 运费模板与规则暂缺
			Total total = scinfo.getTotal();
			Total totalall = getitemsordercall.getTotal();
			Logger.verifyEquals(0,
					new BigDecimal(productinfomap.get("Fflight").toString()).compareTo(total.getFreight()),
					"验证total Freight");
			Logger.verifyEquals(0, Price.getOneActprice(activityInfo, 0, false).compareTo(total.getPrice()),
					"验证total Price");
			Logger.verifyEquals(0, total.getPrice().add(total.getFreight()).compareTo(total.getSum()), "验证total sum");
			// 只有一个商品，两层total应该一样
			Logger.verifyEquals(total.getFreight(), totalall.getFreight(), "验证total Freight");
			Logger.verifyEquals(total.getPrice(), totalall.getPrice(), "验证total Price");
			Logger.verifyEquals(total.getSum(), totalall.getSum(), "验证total Sum");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_002() {
		Logger.start(true,"不输入userid查询获取购物车拆单后信息失败");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			Map m = ShoppingCartCallServices.addCatalogToCart(uid);
			// Logger.comment("setUserID："+uid);
			// getitemsorderbean.setUserID(uid);
			// 310101 上海 黄浦区
			Logger.comment("setAreaCode：310101");
			getitemsorderbean.setAreaCode("310101");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemsordercall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemsordercall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, getitemsordercall.getMsg().contains(ECode.ERROR400_MSG_1), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_003() {
		Logger.start(true,"不输入areacode查询获取购物车拆单后信息失败");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			Map m = ShoppingCartCallServices.addCatalogToCart(uid);
			Logger.comment("setUserID：" + uid);
			getitemsorderbean.setUserID(uid);
			// 310101 上海 黄浦区
			// Logger.comment("setAreaCode：310101");
			// getitemsorderbean.setAreaCode("310101");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemsordercall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemsordercall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, getitemsordercall.getMsg().contains(ECode.ERROR400_MSG_2), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_004() {
		Logger.start(true,"输入userid和GuestUserID查询合并后购物车拆单后信息成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			Map m = ShoppingCartCallServices.addCatalogToCart(uid);
			String uid2 = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid2：" + uid2);
			Map m2 = ShoppingCartCallServices.addCatalogToCart(uid2);
			Logger.comment("setUserID：" + uid);
			getitemsorderbean.setUserID(uid);
			Logger.comment("setGuestUserID：" + uid2);
			getitemsorderbean.setGuestUserID(uid2);
			// 310101 上海 黄浦区
			Logger.comment("setAreaCode：310101");
			getitemsorderbean.setAreaCode("310101");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemscall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemscall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			// 两者加的是一个商品，数量叠加
			if (m.get("CatalogId").toString().equals(m2.get("CatalogId").toString())) {
				ShoppingCartItems1 scinfo = getitemsordercall.getShoppingCartItems().get(0);
				ShoppingCartItems2 scinfo2 = scinfo.getShoppingCartItems().get(0);
				Catalog ca = scinfo2.getCatalog();
				// 验证 Catalog
				Logger.verifyEquals(m.get("CatalogId"), ca.getID(), "验证catalogid");
				Logger.verifyEquals(m.get("ProductId"), ca.getProductId(), "验证ProductId");
				Logger.verifyEquals(m.get("ProductName"), ca.getName(), "验证Name");
				Logger.verifyEquals(Double.valueOf(m.get("fQuotePrice").toString()), ca.getPrice(), "验证Price");
				Logger.verifyEquals(2, scinfo2.getCount(), "验证count");
			} else {
				ShoppingCartItems1 scinfo = getitemsordercall.getShoppingCartItems().get(0);
				List<ShoppingCartItems2> scinfo2list = scinfo.getShoppingCartItems();
				for (int i = 0; i < scinfo2list.size(); i++) {
					Catalog ca = scinfo2list.get(i).getCatalog();
					// 验证 Catalog
					if (m.get("CatalogId").toString().equals(ca.getID())) {
						Logger.verifyEquals(1, scinfo2list.get(i).getCount(), "验证count");
						Logger.verifyEquals(m.get("CatalogId"), ca.getID(), "验证catalogid");
						Logger.verifyEquals(m.get("ProductId"), ca.getProductId(), "验证ProductId");
						Logger.verifyEquals(m.get("ProductName"), ca.getName(), "验证Name");
						Logger.verifyEquals(Double.valueOf(m.get("fQuotePrice").toString()), ca.getPrice(), "验证Price");
					} // 分开验证，循环获取 匹配 Catalogid 验证
					else if (m2.get("CatalogId").toString().equals(ca.getID())) {
						Logger.verifyEquals(1, scinfo2list.get(i).getCount(), "验证count");
						Logger.verifyEquals(m2.get("CatalogId"), ca.getID(), "验证catalogid");
						Logger.verifyEquals(m2.get("ProductId"), ca.getProductId(), "验证ProductId");
						Logger.verifyEquals(m2.get("ProductName"), ca.getName(), "验证Name");
						Logger.verifyEquals(Double.valueOf(m2.get("fQuotePrice").toString()), ca.getPrice(), "验证Price");
					}
				}
				// 验证总价
				Total totalall = getitemsordercall.getTotal();
				BigDecimal Fflight = new BigDecimal(m.get("fFlight").toString()).add(new BigDecimal(m2.get("fFlight")
						.toString()));
				BigDecimal price = Price.getOneActprice(m, 0, false).add(Price.getOneActprice(m2, 0, false));
				Logger.verifyEquals(0, Fflight.compareTo(totalall.getFreight()), "验证total Freight");
				Logger.verifyEquals(0, price.compareTo(totalall.getPrice()), "验证total Price");

			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_005() {
		Logger.start(true,"输入IsUserNewbie为true查询获取购物车拆单后信息为新人价格成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID：" + uid);
			getitemsorderbean.setUserID(uid);
			// 310101 上海 黄浦区
			Logger.comment("setAreaCode：310101");
			getitemsorderbean.setAreaCode("310101");
			Logger.comment("setIsUserNewbie：true");
			getitemsorderbean.setIsUserNewbie("true");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemsordercall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemsordercall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			// 计算商户的总价
			ShoppingCartItems1 scinfo = getitemsordercall.getShoppingCartItems().get(0);
			Total t = scinfo.getTotal();
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, 0, true);
			Logger.verifyEquals(Double.valueOf(actprice.toString()), Double.valueOf(t.getPrice().toString()),
					"验证返回Price");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_006() {
		Logger.start(true,"输入UserLevel为3查询用户等级为3的优惠信息成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID：" + uid);
			getitemsorderbean.setUserID(uid);
			// 310101 上海 黄浦区
			Logger.comment("setAreaCode：310101");
			getitemsorderbean.setAreaCode("310101");
			Logger.comment("setUserLevel：3");
			getitemsorderbean.setUserLevel("3");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemsordercall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemsordercall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			// 计算商户的总价
			ShoppingCartItems1 scinfo = getitemsordercall.getShoppingCartItems().get(0);
			Total t = scinfo.getTotal();
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, 3, false);
			Logger.verifyEquals(Double.valueOf(actprice.toString()), Double.valueOf(t.getPrice().toString()),
					"验证返回Price");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartItemsForOrder_007() {
		Logger.start(true,"输入UserLevel为6查询用户等级为6的优惠信息成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID：" + uid);
			getitemsorderbean.setUserID(uid);
			// 310101 上海 黄浦区
			Logger.comment("setAreaCode：310101");
			getitemsorderbean.setAreaCode("310101");
			Logger.comment("setUserLevel：6");
			getitemsorderbean.setUserLevel("6");
			getitemsordercall.setData(getitemsorderbean);
			getitemsordercall.callService();
			Logger.comment("getitemsordercall send:" + getitemsordercall.getEntityString());
			Logger.comment("getitemsordercall return:" + getitemsordercall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getitemsordercall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, getitemsordercall.getBCode(), "验证返回bcode");
			// 计算商户的总价
			ShoppingCartItems1 scinfo = getitemsordercall.getShoppingCartItems().get(0);
			Total t = scinfo.getTotal();
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, 6, false);
			Logger.verifyEquals(Double.valueOf(actprice.toString()), Double.valueOf(t.getPrice().toString()),
					"验证返回Price");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}